package com.ztao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.common.admin.dto.SensitiveDto;
import com.heima.model.common.admin.pojos.WmSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.SensitiveWordUtil;
import com.ztao.mapper.SensitiveMapper;
import com.ztao.service.SensitiveService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SensitiveServiceImpl implements SensitiveService {
    @Resource
    private SensitiveMapper sensitiveMapper;
    //查看所有敏感词
    @Override
    public ResponseResult findAll_Sensitive(SensitiveDto dto) {
        IPage<WmSensitive> page=new Page<>(dto.getPage(),dto.getSize());
        QueryWrapper<WmSensitive> wrapper=new QueryWrapper<>();
        wrapper.like("sensitives",dto.getName());
        wrapper.lambda().orderByDesc(WmSensitive::getCreatedTime);
        page=this.sensitiveMapper.selectPage(page,wrapper);
        ResponseResult result=new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        result.setData(page.getRecords());
        System.out.println(result+"11111111111111111111111");
        return result;
    }

    //添加敏感词
    @Override
    public ResponseResult add_Sensitive(WmSensitive wmSensitive) {
        WmSensitive sensitives = this.sensitiveMapper.selectOne(new QueryWrapper<WmSensitive>()
                .eq("sensitives", wmSensitive.getSensitives()));
        if(sensitives==null){
            wmSensitive.setCreatedTime(new Date());
            this.sensitiveMapper.insert(wmSensitive);
            return ResponseResult.okResult(200);
        }
        return  ResponseResult.errorResult(500,"敏感词已存在");

    }

    @Override
    public ResponseResult update_Sensitive(WmSensitive wmSensitive) {
        this.sensitiveMapper.updateById(wmSensitive);
        return ResponseResult.okResult(200);
    }

    @Override
    public ResponseResult delete_Sensitive(Integer id) {
        this.sensitiveMapper.deleteById(id);
        return ResponseResult.okResult(200);
    }

    @Resource
    private SensitiveMapper wmSensitiveMapper;

    @Override
    public ResponseResult checkSensitive(String content) {
        // 获取所有的敏感词
        List<WmSensitive> wmSensitives = wmSensitiveMapper.selectList(Wrappers.<WmSensitive>lambdaQuery().select(WmSensitive::getSensitives));
        List<String> sensitiveList = wmSensitives.stream().map(WmSensitive::getSensitives).collect(Collectors.toList());

        // 初始化敏感词库
        SensitiveWordUtil.initMap(sensitiveList);

        // 查看评论中是否包含敏感词
        Map<String, Integer> map = SensitiveWordUtil.matchWords(content);
        if(map.size() > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "当前评论中包含敏感词: " + map);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
